﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
using System.Threading;
namespace weiqijiance.灯光仪
{
    public class QDC_1C : libass
    {
        public override bool openCOM(string sSet)
        {
            bool breturn = true;
            try
            {
                base.openCOM(sSet);
                comLI.ReadTimeout = 500;

                comLI.DataReceived += comLI_DataReceived;
            }
            catch
            {
                breturn = false;
            }
            return breturn;
        }

        public override bool sendLIcom(int cmd)//发送指令给灯光仪串口
        {
            bool flag = true;

            try
            {
                switch (cmd)
                {
                    case 1: { sendcmd("0x80"); break; }       // 1:左靠位命令;
                    case 2: { sendcmd("0x81"); break; }         // 2:右靠位命令;
                    case 3: { sendcmd("0x32"); break; }     // 3:二灯制;
                    case 4: { sendcmd("0x31"); break; }         // 4:四灯制;
                    case 5: { sendcmd("0x33"); break; }         // 5:单检测远光;
                    //case 6: { sendcmd("0x34"); break; }     // 6:单检近光;
                    //case 7: { sendcmd("0x35"); break; }     // 7:远近光均检;
                    case 8: { sendcmd(""); break; }     // 8:先测远灯，后测近灯;
                    case 9: { sendcmd("0x07"); break; }         // 9:测量左灯;
                    case 10: { sendcmd("0x41"); break; }        // 10:测量右灯;
                    case 11: { sendcmd("0x34"); break; }        // 11:取左主灯数据;
                    case 12: { sendcmd("0x4D"); break; }        // 12:取右主灯数据
                    case 13: { sendcmd("0x47"); break; }        // 13:仪器归位;
                    case 14: { sendcmd("0x3A"); break; }        // 14:向左移动
                    case 15: { sendcmd("0x3B"); break; }        // 15:向右移动
                    case 16: { sendcmd("0x3E"); break; }        // 16:停止移动
                    case 17: { sendcmd("0x51"); break; }        // 17:原地检测
                    case 18: { sendcmd("0x4E"); break; }        // 18:当前最后一次
                }

            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        public bool sendcmd(string sneirong)//发送指令给灯光仪串口
        {
            bool flag = true;
            try
            {
                string hexValues = sneirong;
                string[] hexValuesSplit = hexValues.Split(' ');
                int icount = 100;
                icount = hexValuesSplit.Length;
                if (comLI.IsOpen)
                {
                    Byte[] m_byte = new Byte[icount];
                    for (int i = 0; i < icount; i++)
                    {
                        m_byte[i] = (byte)Convert.ToInt32(hexValuesSplit[i], 16);
                    }
                    comLI.Write(m_byte, 0, icount);
                }
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        //灯光 返回数据
        void comLI_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(300);
            int n = comLI.BytesToRead;
            byte[] buf = new byte[n];
            comLI.Read(buf, 0, n);

            if (n ==20)
            {
                if (buf[0] == 0x31)
                {
                    string data = Encoding.Default.GetString(buf);
                    int.TryParse(data.Substring(13, 3), out iLIGQ);
                    iLIGQ = iLIGQ * 100;
                    bqushu = true;
                }

            }
            else if (n == 1)
            {
                switch (buf[0])
                {
                    case 0x4F: byuanguang = true;
                        break;
                    case 0x40: bguiwei = true;
                        break;
                    case 0x13: bjcshibai = true;
                        break;
                    default:
                        break;
                }

            }
        }
    }
}
